// pages/print/library/library.js
var utils = require("../../utils/utils.js");
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chooseIndex:'',
    classList:[],
    list:[],
    currentObj:{
      currentColor:1,
      currentNum:1,
      currentFace:1,
      currentPaper:'30523606',
    },
    docFixShow:false,   //设置文档view
    num:1,
    refreshState:false,
    page:1,
    pageNum:10,
    key:'',
    currentPrice:0.00,
    isFree:0,
    sxFixShow:false,
    nextType:'',
    firstOpen:0
  },
  showSx(){
    this.setData({
      sxFixShow:true
    })
  },
  hideSx(){
    this.setData({
      sxFixShow:false
    })
  },
  changeIndex(e){
    var code = e.currentTarget.dataset.code;
    this.setData({
      chooseIndex:code,
      page:1,
      nextType:''
    })
    this.getList()
    this.getNextClass()
  },
  chooseNextType(e){
    var code = e.currentTarget.dataset.code;
    this.setData({
      nextType:code,
      page:1
    })
    this.getList();
  },
  resetBtn(){
    this.setData({
      nextType:''
    })
    this.getList();
  },
  //获取下级分类
  getNextClass(){
    var that = this;
    utils.request('Index/tempTypeAllNext',{type:that.data.chooseIndex}, function (res) {
      if(res.data.code==1){
        that.setData({
            nextClass:res.data.data
        })
      }else{
        utils.toast(res.data.msg)
      }
    })
  },
  //获取文库分类
  getClass(){
    var that = this;
    utils.request('Index/tempList',{}, function (res) {
      if(res.data.code==1){
        that.setData({
          classList:res.data.data.type,
          chooseIndex:that.data.chooseIndex?that.data.chooseIndex:res.data.data.type[0].code,
          payTypeList:res.data.data.payTypeList
        })
        that.getList()
        that.getNextClass()
      }else{
        utils.toast(res.data.msg)
      }
    })
  },
  //预览
  preview(e){
    var that = this;
    var url = e.currentTarget.dataset.url;
    var title = e.currentTarget.dataset.title;
    if(utils.is_define(url)){
      if(that.data.firstOpen==0){
        wx.showModal({
          title: '预览提示',
          content:'预览为有水印版，正式打印为无水印版',
          showCancel:false,
          confirmText:'我知道了',
          success(res) {
            that.setData({
              firstOpen:1
            })
            wx.hideLoading()
            wx.navigateTo({
                url: '/pages/my/pdfsee/pdfsee?url='+url+'&name='+title
            })
            // wx.showLoading()
            // wx.downloadFile({
            //   // 示例 url，并非真实存在
            //   url:url,
            //   success: function (res) {
            //     const filePath = res.tempFilePath
            //     wx.openDocument({
            //       filePath: filePath,
            //       showMenu:true,
            //       success: function (res) {
            //         wx.hideLoading()
            //       },
            //       fail(err){
            //         wx.hideLoading()
            //       }
            //     })
            //   }
            // })
          }
        })
      }else{
        wx.navigateTo({
            url: '/pages/my/pdfsee/pdfsee?url='+url+'&name='+title
        })
      }
    }else{
      utils.toast('当前文档暂不支持预览')
    }
  },
  addKey(e){
    this.setData({
      key:e.detail.value
    })
  },
  searchVal(){
    this.setData({
      page:1
    })
    this.getList();
  },
  //获取文库列表
  getList(){
    var that = this;
    var type = that.data.chooseIndex;
    var array = that.data.list;
    var listArray = [];
    if(utils.is_define(that.data.nextType)){
      type = that.data.nextType;
    }
    utils.request('Index/tempList',{type:type,page:that.data.page,page_num:that.data.pageNum,keyword:that.data.key,isfree:that.data.isFree}, function (res) {
      if(res.data.code==1){
        if(that.data.page==1){
          listArray = res.data.data.list;
        }else{
          listArray = array.concat(res.data.data.list);
        }
        if(listArray.length>0){
          that.setData({
            list:listArray,
            refreshState:false
          })
        }else{
          that.setData({
            list:[],
            refreshState:false
          })
        }
      }else{
        utils.toast(res.data.msg)
      }
    })
  },
  goList(){
    wx.navigateTo({
      url: '../list/list',
    })
  },
    //提交订单到支付页
    confirmOrder(id,price){
      var that = this;
      wx.navigateTo({
        url: '../print/pay/pay?taskNo='+id+'&payTypeList='+JSON.stringify(that.data.payTypeList)+'&price='+price+'&type=wk',
      })
    },
    changeFree(e){
      var index = e.currentTarget.dataset.index;
      this.setData({
        isFree:index,
        page:1
      })
      this.getList()
    },
  //打开文档设置
  showDocFix(e){
    var that = this;
    var pay = e.currentTarget.dataset.pay;
    var id = e.currentTarget.dataset.id;
    var price = e.currentTarget.dataset.price;
      var obj = this.data.currentObj
      obj.code = e.currentTarget.dataset.id;
      this.setData({
        docFixShow:true,
        currentObj:obj,
        currentPrice:price
      })
  },
  //关闭文档设置
  hideDocFix(){
    this.setData({
      docFixShow:false
    })
  },
  //添加进购物车
  addCart(){
    var that = this;
    var obj = that.data.currentObj;
    app.get_local('AccToken').then(res =>{
      if(utils.is_define(res)){
        app.get_local('machineQrCode').then(res => {
          if(utils.is_define(res)){
            wx.showLoading()
            var data = {
              tempId:obj.code,
              num:obj.currentNum,
              paper:obj.currentPaper,
              isColor:obj.currentColor,
              isDouble:obj.currentFace,
              sn:res
            }
            utils.request('Order/tempPrint',data, function (res) {
              wx.hideLoading();
              that.setData({
                docFixShow:false
              })
              if(res.data.code==1){
                wx.navigateTo({
                  url: '../print/list/list',
                })
              }else if(res.data.code==-1){
                wx.showModal({
                  title: '提示',
                  content: '此文档为付费文档，确定要支付吗？',
                  confirmText:'支付',
                  success (res) {
                    if (res.confirm) {
                      that.confirmOrder(obj.code,that.data.currentPrice)
                    }
                  }
                })
              }else{
                utils.toast(res.data.msg)
              }
            })
          }else{
            utils.toast("请先选择打印机");
          }
        })
      }else{
        wx.navigateTo({
          url: '../my/login/login',
        })
      }
    })
  },
  // 数量变化
  add() {
    var num = this.data.currentObj.currentNum;
    num++;
    var obj = this.data.currentObj;
    obj.currentNum = num;
    this.setData({
      currentObj:obj
    })
  },
  lessen() {
    var num = this.data.currentObj.currentNum;
    if (num == 1) {
      utils.toast('最小份数为1')
      return false;
    }
    num--;
    var obj = this.data.currentObj;
    obj.currentNum = num;
    this.setData({
      currentObj:obj
    })
  },
  //单双面
  changeFace(e){
    var index = e.currentTarget.dataset.index;
    var obj = this.data.currentObj;
    obj.currentFace = index;
    this.setData({
      currentObj:obj
    })
  },
  //色彩
  changeColor(e){
    var index = e.currentTarget.dataset.index;
    var obj = this.data.currentObj;
    obj.currentColor = index;
    this.setData({
      currentObj:obj
    })
  },
  // 纸张
  changePaper(e){
    var index = e.currentTarget.dataset.index;
    var obj = this.data.currentObj;
    obj.currentPaper = index;
    this.setData({
      currentObj:obj
    })
  },
  docConfirm(){
    
  },
    //设置个空白方法，阻止冒泡
    touchAnother(){

    },
    scrollTop (){
      this.setData({
        refreshState:true,
        page:1
      })
      this.getList();
    },
    scrollBottom(){
      var that = this;
      var page = parseInt(that.data.page)+1
      that.setData({
        page:page
      })
      that.getList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
        chooseIndex:options.code
    })
    this.getClass();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})